package pCloud.server;

import java.io.IOException;

import pCloud.PCloudFactory;
import pCloud.config.PCloudConfig;
import pCloud.service.CachingHandle;
import pCloud.service.IncompleteResponse;
import pCloud.service.ProtocolHandle;
import pCloud.service.ProxyServerClientProtHandle;
import pCloud.transport.Communicator;
import pCloud.transport.EndPointTransportHandle;
import pCloud.transport.SocketServerTransportHandle;

public class ProxyServerAcceptor extends Acceptor {

	CachingHandle cachingHandler;
	IncompleteResponse incompleteResponses;

	public ProxyServerAcceptor(int port, Communicator comm,
			CachingHandle cachingHandler, IncompleteResponse ir)
			throws Exception {
		super(port, comm);
		this.cachingHandler = cachingHandler;
		this.incompleteResponses = ir;
	}

	@Override
	public EndPointTransportHandle getServerTransportHandle(int port)
			throws Exception {
		EndPointTransportHandle ep = PCloudFactory
				.getEndPointTransportHandle(PCloudConfig
						.getString(PCloudConfig.PCLOUD_TRANSPORT_SERVER));
		ep.initalize("", port);
		return ep;
	}

	@Override
	public ProtocolHandle getNewProtocolHandle() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException {
		ProxyServerClientProtHandle ph = (ProxyServerClientProtHandle) PCloudFactory
				.getNewProtocolHandleInstance(PCloudConfig
						.getString(PCloudConfig.PCLOUD_PROXY_SERVER_PROT));

		if (PCloudConfig.containsField(PCloudConfig.PCLOUD_CACHE_OPTION)
				&& PCloudConfig.getBoolean(PCloudConfig.PCLOUD_CACHE_OPTION))
			ph.initializeCacheHandler(this.cachingHandler);

		ph.initializeIncompleteResponse(this.incompleteResponses);

		return ph;
	}
}
